package com.monkey.tensquare.recruit.controller;

import com.monkey.tensquare.recruit.entity.Enterprise;
import com.monkey.tensquare.recruit.service.EnterpriseService;
import entity.PageResult;
import entity.Result;
import entity.StateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/enterprise")
public class EnterpriseController {
    @Autowired
    private EnterpriseService enterpriseService;
    @PostMapping
    public Result add(@RequestBody Enterprise enterprise){
        try {
            enterpriseService.add(enterprise);
            return new Result(StateCode.OK,true,"添加成功！",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"添加失败！",null);
        }
    }
    @GetMapping
    public Result findAll(){
        try {
            List<Enterprise> enterpriseList = enterpriseService.findAll();
            return new Result(StateCode.OK,true,"查询所有企业成功！",enterpriseList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"查询所有企业失败！",null);
        }
    }
    @GetMapping("{enterpriseId}")
    public Result findById(@PathVariable String enterpriseId){
        try {
            Enterprise enterprise = enterpriseService.findById(enterpriseId);
            return new Result(StateCode.OK,true,"查询企业成功！",enterprise);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"查询企业失败！",null);
        }
    }
    @PutMapping("{enterpriseId}")
    public Result update(@RequestBody Enterprise enterprise, @PathVariable String enterpriseId){
        try {
            enterpriseService.update(enterpriseId,enterprise);
            return new Result(StateCode.OK,true,"修改企业成功！",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"修改企业失败！",null);
        }
    }
    @DeleteMapping("{enterpriseId}")
    public Result delete(@PathVariable String enterpriseId){
        try {
            enterpriseService.delete(enterpriseId);
            return new Result(StateCode.OK,true,"删除企业成功！",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"删除企业失败！",null);
        }
    }
    @PostMapping("search")
    public Result search(@RequestBody Enterprise enterprise){
        try {
            List<Enterprise> enterpriseList = enterpriseService.search(enterprise);
            return new Result(StateCode.OK,true,"搜索企业成功！",enterpriseList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"搜索企业失败！",null);
        }
    }
    @PostMapping("search/{page}/{size}")
    public Result search(@RequestBody Enterprise enterprise, @PathVariable Integer page, @PathVariable Integer size){
        try {
            PageResult<Enterprise> enterpriseList = enterpriseService.search(enterprise, page, size);
            return new Result(StateCode.OK,true,"分页搜索企业成功！",enterpriseList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"分页搜索企业失败！",null);
        }
    }
    @PostMapping("search/hotlist")
    public Result hotlist(){
        try {
            List<Enterprise> enterpriseList = enterpriseService.hotlist();
            return new Result(StateCode.OK,true,"搜索热门企业成功！",enterpriseList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR,false,"搜索热门企业失败！",null);
        }
    }
}
